import { getUserInfo } from '@/api/user';
import { message } from 'ant-design-vue';
import { defineStore } from 'pinia';
import { ref } from 'vue';
import type { IUserInfo } from '../interface/user';

export const useUserStore = defineStore('user', () => {
  const userInfo = ref<IUserInfo>({} as IUserInfo);
  function setUserInfo(info: IUserInfo) {
    userInfo.value = info;
  }

  function fetchUserInfo() {
    return new Promise((resolve, reject) => {
      getUserInfo()
        .then(res => {
          console.log('🚀 ~ fetchUserInfo ~ res:', res);
          userInfo.value = res as IUserInfo;
          resolve(res);
        })
        .catch(err => {
          message.error('获取用户信息失败');
          reject(err);
        });
    });
  }

  return { userInfo, setUserInfo, fetchUserInfo };
});
